package org.infodavid.common.persistence;

import java.util.Collection;

import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.infodavid.common.model.IApplicationSettings;
import org.infodavid.common.model.IGenericProperty;
import org.infodavid.common.model.IOrganization;
import org.infodavid.common.model.criteria.IGenericPropertyCriteria;
import org.infodavid.common.persistence.impl.AbstractDataService;
import org.infodavid.common.reflect.InstantiationHelper;
import org.joda.time.DateTime;

/**
 * The Class ApplicationSettingsDataService.
 */
public abstract class ApplicationSettingsDataService extends
    AbstractDataService<IApplicationSettings,Long> {

  /** The singleton. */
  private static final ApplicationSettingsDataService SINGLETON;

  /** The Constant log. */
  private static final Log LOGGER = LogFactory.getLog(ApplicationSettingsDataService.class);

  static {
    try {
      SINGLETON =
          InstantiationHelper.getInstance().newInstance(ApplicationSettingsDataService.class);
    }
    catch (final Exception e) {
      LOGGER.fatal("Can not instantiate the " + ApplicationSettingsDataService.class.getName(), e);

      throw new Error(e);
    }
  }

  /**
   * returns the singleton.
   * @return the singleton
   */
  public static ApplicationSettingsDataService getInstance() {
    return SINGLETON;
  }

  /**
   * Instantiates a new service.
   */
  protected ApplicationSettingsDataService() {
    super();
  }

  /**
   * Find first settings.
   * @param session the session
   * @return the organization
   * @throws PersistenceException the persistence exception
   */
  public abstract IApplicationSettings findFirst(PersistenceSession session)
      throws PersistenceException;

  /**
   * Find organization.
   * @param session the session
   * @return the organization
   * @throws PersistenceException the persistence exception
   */
  public abstract IOrganization findOrganization(PersistenceSession session)
      throws PersistenceException;

  /**
   * Do find by name.
   * @param session the session
   * @param value the value
   * @return the group
   * @throws PersistenceException the persistence exception
   */
  public abstract IApplicationSettings findByProduct(PersistenceSession session, String value)
      throws PersistenceException;

  /**
   * Find not null property value.
   * @param session the session
   * @param property the property
   * @return the string
   * @throws PersistenceException the persistence exception
   */
  public abstract String findNotNullPropertyValue(PersistenceSession session, String property)
      throws PersistenceException;

  /**
   * Find the properties using the scope.
   * @param context the context
   * @param product the short name of the product
   * @param criteria the criteria
   * @param results the resulting properties
   */
  public abstract void findProperties(PersistenceSession session,
      Collection<IGenericProperty> results, String product, IGenericPropertyCriteria criteria)
      throws PersistenceException;

  /**
   * Sets the properties.
   * @param context the context
   * @param product the short name of the product
   * @param properties the properties to update
   * @return the modification associated to the application settings entity
   */
  public abstract DateTime updateProperties(PersistenceSession session, String product,
      Collection<IGenericProperty> properties) throws PersistenceException;
}
